﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ragipo
{
    public static class LastfmApi
    {

        #region Champs 

        private static string url_EventsByArtist = "http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=#artiste#&api_key=d6c6a8063c47fd6c9f28b014647e2a44";

        private static string url_EventsByVille = "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=#ville#&api_key=d6c6a8063c47fd6c9f28b014647e2a44";

        private static string url_getAllArtistsByName = "http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=#nomArtist#&api_key=d6c6a8063c47fd6c9f28b014647e2a44";
      

        #endregion

        #region Méthode 

        public static List<Event> getEventsByArtist(string nomArtiste)
        {
            List<Event> lesEvents = new List<Event>();

            string url_EventsByArtiste = url_EventsByArtist.Replace("#artiste#", nomArtiste);
            IEnumerable<XElement> xLesEvents = XDocument.Load(url_EventsByArtiste).Root.Element("events").Elements("event");

            int i = 1;

            foreach (XElement xUnEvent in xLesEvents)
            {
                XElement xUneVenue = xUnEvent.Element("venue");
                XElement xUneLocation = xUneVenue.Element("location");
                Location uneLocation = new Location(xUneLocation.Element("street").Value,
                                                    xUneLocation.Element("city").Value,
                                                    xUneLocation.Element("country").Value,
                                                    xUneLocation.Element("postalcode").Value);

                Venue uneVenue = new Venue(xUneVenue.Element("name").Value, xUneVenue.Element("url").Value, uneLocation);

                Artist unArtist = new Artist(nomArtiste);

                Event unEvent = new Event(xUnEvent.Element("id").Value,
                                       xUnEvent.Element("title").Value,
                                       xUnEvent.Element("description").Value,
                                       xUnEvent.Element("startDate").Value,
                                       xUnEvent.Element("image").Value,
                                       uneVenue,
                                       unArtist);

                lesEvents.Add(unEvent);
                i++;

            }


            return lesEvents;
        
        }
        public static List<Artist> getAllArtistByName(string nomArtist)
        {
            List<Artist> lesArtists = new List<Artist>();

            string url_ArtistByName = url_getAllArtistsByName.Replace("#nomArtist#", nomArtist);

            int i = 1;

            IEnumerable<XElement> xLesArtists = XDocument.Load(url_ArtistByName).Root.Element("results").Element("artistmatches").Elements("artist");

            foreach (XElement xUnArtist in xLesArtists)
            {
                Artist unArtist = new Artist(xUnArtist.Element("mbid").Value,
                                             xUnArtist.Element("name").Value,
                                             xUnArtist.Element("url").Value,
                                             xUnArtist.Element("image").Value);
                lesArtists.Add(unArtist);
                i++;
            }

            return lesArtists;
        }




        public static List<Event> getGeoEvents(string eLocation)
        {
            List<Event> lesEvents = new List<Event>();

            string url_ConcertsParSalle = url_EventsByVille.Replace("#ville#", eLocation);

            IEnumerable<XElement> xLesEvents = XDocument.Load(url_ConcertsParSalle).Root.Element("events").Elements("event");

            int i = 1;

            foreach (XElement xUnEvent in xLesEvents)
            {
                XElement xUneVenue = xUnEvent.Element("venue");
                XElement xUneLocalisation = xUneVenue.Element("location");
                Location uneLocalisation = new Location(xUneLocalisation.Element("street").Value,
                                                                xUneLocalisation.Element("city").Value,
                                                                xUneLocalisation.Element("country").Value,
                                                                xUneLocalisation.Element("postalcode").Value);

                Venue uneVenue = new Venue(xUneVenue.Element("name").Value, xUneVenue.Element("url").Value, uneLocalisation);

                Event thisEvent = new Event(xUnEvent.Element("id").Value,
                                       xUnEvent.Element("title").Value,
                                       xUnEvent.Element("description").Value,
                                       xUnEvent.Element("startDate").Value,
                                       xUnEvent.Element("image").Value,
                                       uneVenue);

                lesEvents.Add(thisEvent);
                i++;

            }
            return lesEvents;
        }











                    
#endregion 
    
       
    }
}
